﻿using ProjectFootball.MVC.Areas.PersonArea.Models;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _PersonExtensions
    {
        public static IEnumerable<PersonSearchResultViewModel> Search(this IQueryable<Person> people, IPrincipal user, SearchType searchType)
        {
            var query = people.Where(p => !p.IsMarkedForDeletion).AsEnumerable().Where(c => c.CanView(user)).OrderBy(c => c.FullName);

            foreach (var person in query)
                yield return person.GetSearchResultViewModel(user, searchType);
        }

        public static PersonSearchResultViewModel GetSearchResultViewModel(this Person person, IPrincipal user, SearchType searchType)
        {
            return new PersonSearchResultViewModel()
            {
                Person = person,
                IsVisible = person.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? person.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !person.IsApproved)) ? person.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? person.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? person.CanApprove(user) : false
            };
        }

        public static PersonDataModel GetDataModel(this Person person)
        {
            return new PersonDataModel()
            {
                PersonGuid = person.Pk,
                ApprovalGuid = person.ApprovalGuid,
                Forenames = person.Forenames,
                Surname = person.Surname,
                Dob = person.Dob,
                CountryGuid = person.CountryGuid,
                ActiveFrom = person.ActiveFrom,
                ActiveTo = person.ActiveTo
            };
        }

        public static void Edit(this Person person, PersonDataModel personDataModel, IPrincipal user)
        {
            person.ApprovalGuid = personDataModel.ApprovalGuid;
            person.Forenames = personDataModel.Forenames;
            person.Surname = personDataModel.Surname;
            person.Dob = personDataModel.Dob;
            person.CountryGuid = personDataModel.CountryGuid;
            person.ActiveFrom = personDataModel.ActiveFrom;
            person.ActiveTo = personDataModel.ActiveTo;
            person.IsApproved = person.IsApproved ? user.IsApprover() : false;
            person.AuditUserId = user.GetUserId();
            person.AuditTimestamp = DateTime.Now;
        }

        public static void Approve(this Person person, Person previousApproved)
        {
            previousApproved.Forenames = person.Forenames;
            previousApproved.Surname = person.Surname;
            previousApproved.Dob = person.Dob;
            previousApproved.CountryGuid = person.CountryGuid;
            previousApproved.ActiveFrom = person.ActiveFrom;
            previousApproved.ActiveTo = person.ActiveTo;
            previousApproved.AuditUserId = person.AuditUserId;
            previousApproved.AuditTimestamp = person.AuditTimestamp;
        }
    }
}